droid 畫圖表的第三方工具
(二)LineChart
導入
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
XML中建立view
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/bar_line"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.github.mikephil.charting.charts.LineChart>
Activity中設定
val linechart = bar_line
設定資料
val entries = ArrayList()
entries.add(Entry(0F,4F))
entries.add(Entry(1f,1f))
entries.add(Entry(2f,2f))
entries.add(Entry(3f,4f))
entries.add(Entry(5f,10f))
設定圖表格式
val dataset = LineDataSet(entries,"Customized values")
dataset.color = ContextCompat.getColor(this,R.color.colorPrimary)
dataset.valueTextColor = ContextCompat.getColor(this,R.color.colorPrimaryDark)
val xAxis = linechart.xAxis
xAxis.position = XAxis.XAxisPosition.BOTTOM
val months = arrayOf("Jan", "Feb", "Mar", "Apr","May","June")
val formatter = IAxisValueFormatter{
value, axis -> months[value.toInt()]
}
xAxis.granularity = 1f
xAxis.valueFormatter = formatter
val yAxisRight = linechart.axisRight
yAxisRight.setEnabled(false)
val yAxisLeft = linechart.axisLeft
yAxisLeft.setGranularity(1f)
// Setting Data
val data = LineData(dataset)
linechart.setData(data)
linechart.animateX(2500)
//refresh
linechart.invalidate()
完工